<?php if (!defined('BASEPATH'))

    exit('No direct script access allowed');

require_once APPPATH . "controllers/api/lcd" . EXT;

session_start();

Class Product extends lcd{

    

	/**

	 * Index Page for this controller.

	 *

	 * Maps to the following URL

	 * 		http://example.com/index.php/welcome

	 *	- or -  

	 * 		http://example.com/index.php/welcome/index

	 *	- or -

	 * Since this controller is set as the default controller in 

	 * config/routes.php, it's displayed at http://example.com/

	 *

	 * So any other public methods not prefixed with an underscore will

	 * map to /index.php/welcome/<method_name>

	 * @see http://codeigniter.com/user_guide/general/urls.html

	 */

     

    function __construct(){

        parent::__construct();

        //$this->load->model('user','',TRUE);

        $this->load->model(array('product_model'));

    }

    //http://localhost:8080/insys3/api/product/productlist/format/json
    function productlist_get()
    {
        $data = $this->product_model->getlist();
        if($data){
            $this->response($data);
        }else{
            $this->response(array('error' => 'User could not be found'), 404);
        }
    }

    //http://localhost:8080/insys3/api/product/productid/id/2/format/json
    function productid_get()
    {
        $data = $this->product_model->getlist();
        if(!$this->get('id'))
        {
            $this->response(NULL, 400);
        }
        $id = $this->get('id');
        $data = $this->product_model->getitem(array('idproduct'=>$id));

        if($data){
            $this->response($data);
        }else{
            $this->response(array('error' => 'User could not be found'), 404);
        }
    }

    //http://localhost:8080/insys3/api/product/categorylist/format/json
    function categorylist_get()
    {
        $data = $this->product_model->getlistcategory();
        if($data){
            $this->response($data);
        }else{
            $this->response(array('error' => 'User could not be found'), 404);
        }
    }

    //http://localhost:8080/insys3/api/product/categoryid/id/2/format/json
    function categoryid_get()
    {
        $data = $this->product_model->getlist();
        if(!$this->get('id'))
        {
            $this->response(NULL, 400);
        }
        $id = $this->get('id');
        $data = $this->product_model->getitemcategory(array('idcategory'=>$id));

        if($data){
            $this->response($data);
        }else{
            $this->response(array('error' => 'User could not be found'), 404);
        }
    }

    




    
    public function add(){

        $data = array(); 
        
        $data = $this->template();
        $data['title_page'] = $data['title_page'].' Add';
         
        $submit = $this->input->post('submit');
        if(!empty($submit)){
            $status = 1;  

            $supplier = $this->input->post('supplier');
            //$supplier = explode('--',$supplier);

            $category = $this->input->post('category');
            $category = explode('--',$category);

            $param['code'] = $this->input->post('code');
            $param['name'] = $this->input->post('name');
            $param['cost'] = $this->input->post('cost');
            $param['sell'] = $this->input->post('sell');
            //$param['idsupplier'] = $supplier[0];
            $param['supplier'] = $supplier;
            $param['idcategory'] = $category[0];
            $param['active'] =  $status;

            $param["datecreated"] = time();
            $param["dateupdated"] = time();
            
            $this->product_model->additem($param);
        }
        $data['list'] = $this->product_model->getlist();
        
        $this->load->view('product/add',$data);  
    
    }

    

    public function update(){

        $data = array();
        
        $data = $this->template();
        $data['title_page'] = $data['title_page'].' Update';
        
        
        $last = $this->uri->total_segments();
        $id = $this->uri->segment($last);
        
        $data['product'] = $this->product_model->getitem(array('idproduct'=>$id));
        //print_r($data['product']);
        
        $submit = $this->input->post('submit');
        if(!empty($submit)){
            $id = $this->input->post('idproduct');

            $supplier = $this->input->post('supplier');
            //$supplier = explode('--',$supplier);

            $category = $this->input->post('category');
            $category = explode('--',$category);

            $param['name'] = $this->input->post('name');
            $param['cost'] = $this->input->post('cost');
            $param['sell'] = $this->input->post('sell');
            //$param['idsupplier'] = $supplier[0];
            $param['idcategory'] = $category[0];
            $param['supplier'] = $supplier;
            $param["dateupdated"] = time();
            
            
            $condition = array('idproduct'=>$id);
            
            $this->product_model->updateitem($param,$condition);
            redirect("product/update/$id", 'refresh');
        }
        
        $data['list'] = $this->product_model->getlist();
        
        $this->load->view('product/update',$data);  

    }

    

    public function delete(){
        $last = $this->uri->total_segments();
        $id = $this->uri->segment($last);
        $data['product'] = $this->product_model->deleteitem(array('idproduct'=>$id));
        redirect("product/view", 'refresh');
    }

// CATEGORY 

    public function add_category(){

        $data = array(); 
        
        $data = $this->template();
        $data['title_page'] = $data['title_page'].' Category Add';
        //$data['list'] = $this->product_model->getlistcategory(); 
        $submit = $this->input->post('submit');
        if(!empty($submit)){
            
            $param['name'] = $this->input->post('name');
            $param['description'] = $this->input->post('detail');    
            $param["datecreated"] = time();
            $param["dateupdated"] = time();
            
            $this->product_model->additemcategory($param);
        }
        $data['list'] = $this->product_model->getlistcategory(); 
        
        $this->load->view('product/add_category',$data);  
    
    }

// CATEGORY VIEW 

    public function view_category(){

        $data = array();

        $data = $this->template();
        $data['title_page'] = $data['title_page'].' Category List';
        
        $data['js'] = $this->load->view('product/js',null,true); 
        
        $data['list'] = $this->product_model->getlistcategory();

        $this->load->view('product/view_category',$data);

    }

    public function update_category(){

        $data = array(); 
        
        $data = $this->template();
        $data['title_page'] = $data['title_page'].' Category Add';
        //$data['list'] = $this->product_model->getlistcategory(); 
        $last = $this->uri->total_segments();
        $id = $this->uri->segment($last);

        $data['category'] = $this->product_model->getitemcategory(array('idcategory'=>$id));

        $submit = $this->input->post('submit');
        if(!empty($submit)){
            $idcategory = $this->input->post('idcategory');
            $param['name'] = $this->input->post('name');
            $param['description'] = $this->input->post('detail');    
            $param["dateupdated"] = time();
            
            $this->product_model->updateitemcategory($param,array('idcategory'=>$idcategory));
        }
        $data['list'] = $this->product_model->getlistcategory(); 
        
        $this->load->view('product/update_category',$data);  
    
    }

    public function delete_category(){
        $last = $this->uri->total_segments();
        $id = $this->uri->segment($last);
        $data['product'] = $this->product_model->deleteitemcategory(array('idcategory'=>$id));
        redirect("product/view_category", 'refresh');
    }

    public function jgetproduct(){
        $value = $this->input->post('name');

        $id = explode("--", $value);
        //echo $id[0];

        $data = array();
        $data['product'] = $this->product_model->getitem(array('idproduct'=>$id[0]));
        echo json_encode($data);
        
    }

}



/* End of file welcome.php */

/* Location: ./application/controllers/welcome.php */